package org.zcl.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.zcl.entity.User;

/**
 * @Package org.zcl.controller
 * @PROJECT_NAME: hystrix-demo
 * @DESCRIPTION:
 * @USER: zhuchenglin
 * @DATE: 2022/2/8 15:24
 */
@RestController
@Slf4j
public class ProducerController {

    @GetMapping(value = "hello/{name}")
    public String hello(@PathVariable("name") String name, Integer mills) {
        log.info("开始执行请求，name: " + name + "要求暂停：" + mills + "毫秒");
        if (mills != null && mills > 0) {
            try {
                Thread.sleep(mills);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return "hello, [" + name + "], this is service producer.....";
    }

    @PostMapping(value = "add")
    public String add(@RequestBody User user) {
        int random = (int) (Math.random() * 100);
        if (random % 2 == 1) {
            throw new RuntimeException();
        }
        String name = user.getName();
        log.info("开始执行请求，name: " + name);
        return "hello, [" + name + "], this is service producer.....";
    }
}
